home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5176 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  50 lines

  1. Path: news.mcgill.ca!noyb
  2. From: noyb@ee470.ee.mcgill.ca (Terence Chiu)
  3. Newsgroups: comp.lang.c
  4. Subject: new C programmer needs help with quicksort
  5. Date: 8 Feb 1996 04:57:17 GMT
  6. Organization: McGill University, Undergraduate EE Lab
  7. Message-ID: <4fbvrd$i6p@sifon.cc.mcgill.ca>
  8. NNTP-Posting-Host: joker.ee.mcgill.ca
  9.  
  10. Hi, I am a new C programmer and I have to right an assignment that sorts an
  11. array in ascending order. The algorithm that I am required to write is that I
  12. must use an auxiliary array instead of repeated exchanges: here is the code
  13. #include <stdio.h>
  14. void quicksort( int table[] ,int first,int last);
  15. void main ()
  16. {  int table[3];
  17.    table[0]=3;
  18.    table[1]=2;
  19.    table[2]=1;
  20.    /*table[3]=2;
  21.    table[4]=1;
  22.    table[5]=4;
  23.    table[6]=5;*/
  24.    quicksort(table,0,2);
  25.    }
  26. void quicksort( int table[] ,int first,int last)
  27.     {int aux[3];
  28.     int  idxhigh, idxlow;
  29.     int pivot, test, temp,i;
  30.     pivot=table[first];
  31.     idxhigh=last; idxlow=first;
  32.  
  33.     for (i=first+1; i<=last; i=i+1)
  34.         {test=table[i];
  35.          if (test < pivot)
  36.            {aux[idxlow]=test;
  37.            idxlow = idxlow+1;}
  38.          if (test > pivot)
  39.            {aux[idxhigh]=test;
  40.             idxhigh = idxhigh-1;}
  41.         }
  42.     aux[idxlow]=pivot;
  43.     
  44.     quicksort(aux,first,idxlow-1);
  45.     quicksort(aux,idxhigh+1,last);
  46.     }
  47.  
  48. for some when the function calls on itself the aux array loses all its current
  49. data. Why? 
  50.